<template>
  <div :class="className" id="main" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts';

const animationDuration = 2000;

export default {

  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "300px"
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      setTimeout(()=>{
 this.initChart();
      },80)
     
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      var chartDom = document.getElementById('main');
      this.chart = echarts.init(chartDom);
      this.setOptions(this.chartData);

    },
    setOptions({ expectedData, date } = {}) {
      this.chart.setOption({
        tooltip: {
          trigger: "axis",
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: "shadow" // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          top: 20,
          left: "2%",
          right: "2%",
          bottom: "3%",
          containLabel: true
        },
        xAxis: [{
          type: "category",
          data: date,
          axisTick: {
            alignWithLabel: true
          },
          axisLine: {
            show:false,
            textStyle: {
              color: '#7C8CA5'
            }
          },
          axisLabel:{
            textStyle: {
              color: '#7C8CA5'
            }
          },
        }],
        yAxis: [{
          type: "value",
          axisTick: {
            show: false
          },
          axisLine: {
            show:false,
            textStyle: {
              color: '#7C8CA5'
            }
          },
          axisLabel:{
            textStyle: {
              color: '#7C8CA5'
            }
          },
        }],
        series: [{
          name: "产量",
          type: "bar",
          stack: "vistors",
          barWidth: "30%",
          data: expectedData,
          animationDuration,
          itemStyle:{
            normal:{
              color:'#2297F0',
              barBorderRadius: [20, 20, 0, 0]
            }
          },
          label: {
            normal: {
              show: true,//开启显示
              position: 'top',//柱形上方
              textStyle: { //数值样式
                color: '#2297F0'
              }
            }
          }
        }]
      });
    }
  }
};
</script>
